<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../static/lib/layui/css/layui.css" media="all" />
  <link rel="stylesheet" href="../../static/css/admin.css" media="all" />
</head>

<body class="tmm-modal-body">
  <div class="layui-container">
    <form class="layui-form" action="">
      <div class="tmm-table-title">原产品：</div>
      <input type="hidden" name="order_sn" value="${order_sn}">
      <input type="hidden" name="time1" value="${time1}">
      <table class="layui-table">
        <colgroup>
          <col width="150">
          <col width="150">
          <col width="200">
          <col>
        </colgroup>
        <thead>
          <tr>
            <th>产品名称</th>
            <th>实收尾款</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>${goods.name}</td>
            <td>
              <input type="text" name="oldMoney" lay-verify="required" id="oldMoney" value="${goods.innerPrice}" placeholder="${goods.innerPrice}"  class="layui-input">
              <input type="hidden" name="oldGoodsName" id="oldGoodsName" value="${goods.name}" class="layui-input">
              <input type="hidden" name="oldGoodsLogo" id="oldGoodsLogo" value="${goods.coverPic}" class="layui-input">
              <input type="hidden" name="oldNum" id="oldNum" value="1" class="layui-input">
              <input type="hidden" name="oldPrice" id="oldPrice" value="${goods.innerPrice}" class="layui-input">
            </td>
          </tr>
        </tbody>
      </table>
      <div class="tmm-table-title" id="afterDiv" style="display: none">变更后产品：</div>
      <table class="layui-table" id="afterTable" style="display: none">
        <colgroup>
          <col width="150">
          <col width="150">
          <col width="200">
          <col>
        </colgroup>
        <thead>
          <tr>
            <th>产品名称</th>
            <th>实收尾款</th>
          </tr>
        </thead>
        <tbody id="editTbody">

        </tbody>
      </table>
      <div class="layui-row">
          <div class="layui-col-md12 layui-layer-btn" style="text-align: center;">
            <a class="tmm-btn" id="edit_product_btn">变更产品</a>
          </div>
        </div>
      <div class="tmm-table-title">新增产品：</div>
      <table class="layui-table" id="addTable">
        <colgroup>
          <col width="150">
          <col width="150">
          <col width="200">
          <col>
        </colgroup>
        <thead>
          <tr>
            <th>产品名称</th>
            <th>实收尾款</th>
            <%--<th>操作</th>--%>
          </tr>
        </thead>
        <tbody id="addTbody">
        </tbody>
      </table>
      <div class="layui-row">
        <div class="layui-col-md12 layui-layer-btn" style="text-align: center;">
          <a class="tmm-btn" id="add_product_btn">新增产品</a>
        </div>
      </div>
      <div class="layui-row">
        <div class="layui-col-md12 tmm-table-summery" id="sumMoney">

        </div>
      </div>
    </form>
  </div>
  <script src="../../static/lib/layui/layui.js"></script>
  <script>
    layui.config({
      base: "../../static/js/"
    }).extend({
      index: 'index'
    }).use(['index', 'form'], function () {
      const $ = layui.$;

      var form = layui.form;
      form.render();

        var productOps = {
            delete: function () {
                if ($(this).parents('tbody').children().length > 1) {
                    $(this).parents('tr').remove();
                }

                return false;
            },
        };

        $("#addTable .tmm-btn").on("click", function () {
            var type = $(this).data("type");
            productOps[type] ? productOps[type].call(this) : "";
        });

      
      $('#add_product_btn').click(function() {
        var pPopIndex=parent.layer.open({
          type: 2,
          title: "新增产品",
          content: "${path}/order/addGoods",
          maxmin: !0,
          area: ["90%", "80%"],
          btn: ["确定新增", "取消"],
          yes: function(index, layero) {

              var body = parent.layer.getChildFrame('body', pPopIndex); //得到iframe页的body内容
              var addTable = body.find("#product_table tbody tr");

              var add = [];
              var flag = false;
              addTable.each(function () {
                  var trType = $(this).attr("data-type");
                  var goods = {};
                  if(trType==0){
                      goods.addId = $(this).find('.layui-select').val();
                      goods.addName = $(this).find('.layui-select').find('option:selected').text();
                      goods.categoryPid = 0;
                      goods.customerName = '';
                      goods.addDoctorId = 0;
                      goods.price = $(this).find('.layui-select').find('option:selected').attr("price");
                  }else{
                      goods.addId = -1;
                      goods.addName = $(this).find('.layui-select').text()
                      goods.categoryPid = $(this).find('.categoryPid').val();
                      goods.customerName = $(this).find('.custom').val();
                      goods.addDoctorId = $(this).find('.doctor').val();
                      goods.price = 0;
                  }

                  if(trType==0){
                      if(goods.addName=="产品名称"){
                          flag = true;
                      }
                  }else{
                      if(goods.customerName == '' || goods.addDoctorId == '' || goods.categoryPid == ''){
                          flag = true;
                      }
                  }

                  add.push(goods);
              })
              if(flag){
                  parent.layer.alert("请录入完整产品信息");
                  return;
              }
              parent.layer.close(index);

              for(var i=0;i<add.length;i++){
                  var name = add[i].addName;
                  $("#addTbody").append('<tr>\n' +
                      '            <td>'+(name!=null&&name!=''?name:add[i].customerName)+'</td>\n' +
                      '            <td>\n' +
                      '              <input type="text" name="addMoney" lay-verify="required" id="addMoney" value="'+add[i].price+'" placeholder="请输入金额" class="layui-input">\n' +
                      '              <input type="hidden" name="addId" value="'+add[i].addId+'" id="addId" placeholder="" class="layui-input">\n' +
                      '              <input type="hidden" name="addCustomName" value="'+(add[i].customerName!=null&&add[i].customerName!=''?add[i].customerName:add[i].addName)+'" id="addCustomName" placeholder="" class="layui-input">\n' +
                      '              <input type="hidden" name="addCategoryPid" value="'+add[i].categoryPid+'" id="categoryPid" placeholder="" class="layui-input">\n' +
                      '              <input type="hidden" name="addDoctorId" value="'+add[i].addDoctorId+'" id="addDoctorId" placeholder="" class="layui-input">\n' +
                      '            </td>\n' +
                      // '<td>'+
                      // '<a class="tmm-btn" data-type="delete">删除</a>'+
                      // '</td>'+
                      '          </tr>')
              }

              parent.layer.close(pPopIndex);
          }
        })
      });
            
      $('#edit_product_btn').click(function() {
        var pPopIndex2 = parent.layer.open({
          type: 2,
          title: "变更产品",
          content: "${path}/order/editGoods?goods_name="+"${goods.name}",
          maxmin: !0,
          area: ["850px", "600px"],
          btn: ["确定变更", "取消"],
          yes: function(index, layero) {
              var body = parent.layer.getChildFrame('body', pPopIndex2); //得到iframe页的body内容
              var trType = body.find("#product_table tbody tr").attr("data-type");

              var customName = body.find("#name3").val();
              var afterDoctorId = body.find("#doctorId").val();
              var categoryPid = body.find("#categoryPid option:selected").val();
              var afterName = body.find("#name1 option:selected").html();
              var afterId = body.find("#name1 option:selected").val();
              var price = body.find("#name1 option:selected").attr("price");
              if(categoryPid==undefined || categoryPid==''){
                  categoryPid = body.find("#name1 option:selected").attr("categoryPid");
              }
              if(price==undefined){
                  price = 0;
              }
              if(afterId==null || afterId==''){
                  afterId=-1;
              }
              if(trType==0){
                  if(afterName=='产品名称'){
                      parent.layer.alert("请录入完整产品信息");
                      return;
                  }
              }else{
                  if(customName == '' || afterDoctorId == '' || categoryPid == ''){
                      parent.layer.alert("请录入完整产品信息");
                      return;
                  }
              }
              parent.layer.close(index);



              $("#afterDiv").show();
              $("#afterTable").show();

              $("#editTbody").empty().append('<tr>\n' +
                  '            <td>'+(customName!=null&&customName!=''?customName:afterName)+'</td>\n' +
                  '            <td>\n' +
                  '              <input type="text" name="afterMoney" lay-verify="required" id="afterMoney" value="'+price+'" placeholder="请输入金额" class="layui-input">\n' +
                  '              <input type="hidden" value="'+afterId+'" name="afterId" id="afterId" placeholder="" class="layui-input">\n' +
                  '              <input type="hidden" value="'+customName+'" name="customName" id="customName" placeholder="" class="layui-input">\n' +
                  '              <input type="hidden" value="'+categoryPid+'" name="categoryPid" id="categoryPid" placeholder="" class="layui-input">\n' +
                  '              <input type="hidden" value="'+afterDoctorId+'" name="afterDoctorId" id="afterDoctorId" placeholder="" class="layui-input">\n' +
                  '            </td>\n' +
                  '          </tr>')
              $("#oldMoney").attr("readonly",true);
              parent.layer.close(pPopIndex2);

          }
        })
      });
      $(function() {
          //输入框的值改变时触发
          $("#money").on("input", function (e) {
              $("#sumMoney").empty().append('合计实收：'+e.delegateTarget.value)
          });
      });
    });
  </script>
</body>

</html>